<?php
namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

/**
 * 用户
 *
 * Class User
 * @package app\models
 *
 * @property integer $id
 * @property string $mobile
 * @property string $nickname
 * @property string $password
 * @property string $auth_key
 * @property string $email
 * @property string $avatar
 * @property integer $type
 * @property integer $status
 * @property integer $create_time
 * @property integer $last_login_type
 *
 * @property UserAccount $account 关联账户
 */
class User extends ActiveRecord implements IdentityInterface
{
    const TYPE_COMMON = 1;
    const TYPE_VIP = 2;

    const STATUS_ACTIVE  = 1;
    const STATUS_STOPED  = 9;
    const STATUS_DELETED = 0;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['type', function($attribute, $params) {
                if (is_array($this->type)) {
                    $this->type = array_sum($this->type);
                }
            }],
            [['mobile', '!password', 'nickname', 'type', 'status'], 'required'],
            [['nickname', 'email'], 'string', 'max'=>256],
            ['mobile', 'unique', 'filter'=>['<>', 'status', User::STATUS_DELETED]],
            ['mobile', 'string', 'max'=>32],
            ['email', 'email'],
            ['status', 'in', 'range'=>[User::STATUS_ACTIVE, User::STATUS_STOPED, User::STATUS_DELETED]],
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return User::findOne($id);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return null;
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * 生成新的auth_key并保存
     */
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString(32);
        $this->save();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($auth_key)
    {
        return $this->auth_key === $auth_key;
    }

    /**
     * 验证用户输入密码
     * @param $password
     * @return bool
     */
    public function validatePassword($password)
    {
        return Yii::$app->security->validatePassword($password, $this->password);
    }

    /**
     * 自动生成对应用户类型详情信息
     * @inheritdoc
     */
    public function afterSave($insert, $changedAttributes)
    {
        if ($insert) {
            // 生成账户信息
            $user_account = new UserAccount();
            $user_account->uid = $this->id;
            $user_account->money = 0;
            $user_account->freeze_money = 0;
            $user_account->save();
        }
        return parent::afterSave($insert, $changedAttributes);
    }

    /**
     * 关联账户
     * @return \yii\db\ActiveQuery
     */
    public function getAccount()
    {
        return $this->hasOne(UserAccount::className(), ['uid'=>'id']);
    }
}
